package thumbs;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import drive.DriveLocations;

import net.coobird.thumbnailator.Thumbnails;

public class Main {
	
	private static File INPUT_FOLDER = new File(DriveLocations.CLYDE + "/Temp/ArtistImages");
	private static File OUTPUT_FOLDER = new File(DriveLocations.CLYDE + "/Temp/ArtistThumbs");
//	private static File INPUT_FOLDER = new File(DriveLocations.CLYDE + "/Photos");
//	private static File OUTPUT_FOLDER = new File(DriveLocations.CLYDE + "/Temp/PhotosThumbs");
	
	public static void main(String[] args) {
		new Main().run();
		System.out.println("DONE.");
	}

	private void run() {
		List<File> folders = new ArrayList<File>();
		folders.add(INPUT_FOLDER);
		
		while (folders.size() > 0) {
			File currentFolder = folders.get(folders.size() - 1);
			folders.remove(folders.size() - 1);
			
			File[] files = currentFolder.listFiles(new java.io.FileFilter() {
				
				@Override
				public boolean accept(File file) {
					String name = file.getName().toLowerCase();
					return file.isDirectory() || name.endsWith(".png")
						|| name.endsWith(".jpg") || name.endsWith(".gif");
				}
			});
			
			for (int i = 0; i < files.length; i++) {
				File currentFile = files[i];
				
				if (currentFile.isDirectory()) {
					folders.add(currentFile);
				} else {
					thumbNail(currentFile);
				}				
			}
		}
	}

	private void thumbNail(File currentFile) {
		System.out.print("Thumbnailing file \"" + currentFile.getName() + "\"...");
		String parentName = currentFile.getParentFile().getName();
		File artistFolder = new File(OUTPUT_FOLDER, parentName);
		artistFolder.mkdirs();
		String shortName = currentFile.getName().substring(0, 
				currentFile.getName().lastIndexOf('.'));
		try {
			Thumbnails.of(currentFile)
			.width(240).outputFormat("png")
			.toFile(new File(artistFolder, shortName + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(" Thumbnailed.");
	}
}
